package avicit.bdp.dgs.qa.utils.tools;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * @金航数码科技有限责任公司
 * @作者：developer
 * @邮箱：developer@avic-digital.com
 * @创建时间： 2023-09-08
 * @类说明：DateUtils
 * @修改记录：
 * @注意事项：
 * @主要功能：日期工具类
 */
public class DateUtils {
    private static final Logger logger = LoggerFactory.getLogger(DateUtils.class);

    /**
     * 获取指定月的每天日期
     * @param startTime
     * @return
     * @throws Exception
     */
    public static List<String> getDayByMonth(String startTime) {
        List<String> dayList = new ArrayList<>();

        try {
            Date month = new SimpleDateFormat("yyyy-MM-dd").parse(startTime);
            Calendar calendar = Calendar.getInstance();
            calendar.clear();
            calendar.setTime(month);

            // 获取指定月天数
            int dayOfMonth = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);

            // 每月每天的时间
            for (int i = 1; i <= dayOfMonth; i++) {
                dayList.add(new SimpleDateFormat("yyyy-MM-dd").format(calendar.getTime()));
                calendar.add(Calendar.DAY_OF_MONTH, 1);
            }
        } catch (Exception e) {
            logger.error("获取指定月的所有日期失败，errMsg：", e);
        }

        return dayList;
    }

    /**
     * 获取指定天的开始时间
     *
     * @return
     */
    public static String getStartTimeOfCurrentDay(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.clear();
        calendar.setTime(date);
        setMinTimeOfDay(calendar);

        Date result = calendar.getTime();
        return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(result);
    }

    /**
     * 获取指定天的结束时间
     *
     * @return
     */
    public static String getEndTimeOfCurrentDay(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.clear();
        calendar.setTime(date);
        setMaxTimeOfDay(calendar);

        Date result = calendar.getTime();
        return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(result);
    }

    /**
     * 获取指定周的开始时间
     *
     * @return
     */
    public static Date getStartTimeOfCurrentWeek(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.clear();
        calendar.setTime(date);
        calendar.setFirstDayOfWeek(Calendar.MONDAY);
        calendar.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
        setMinTimeOfDay(calendar);

        return calendar.getTime();
    }

    /**
     * 获取指定月的结束时间
     *
     * @return
     */
    public static Date getEndTimeOfCurrentWeek(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.clear();
        calendar.setTime(date);
        calendar.setFirstDayOfWeek(Calendar.MONDAY);
        calendar.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
        setMaxTimeOfDay(calendar);

        return calendar.getTime();
    }

    /**
     * 获取指定月的开始时间
     *
     * @return
     */
    public static String getStartTimeOfCurrentMonth(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.clear();
        calendar.setTime(date);
        calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), 1);
        setMinTimeOfDay(calendar);

        Date result = calendar.getTime();
        return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(result);
    }

    /**
     * 获取指定月的结束时间
     *
     * @return
     */
    public static String getEndTimeOfCurrentMonth(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.clear();
        calendar.setTime(date);
        int maxMonthDay = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
        calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), maxMonthDay);
        setMaxTimeOfDay(calendar);

        Date result = calendar.getTime();
        return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(result);
    }

    /**
     * 获取指定年的开始时间 注意月份要减1
     *
     * @return
     */
    public static Date getStartTimeOfCurrentYear(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.clear();
        calendar.setTime(date);
        calendar.set(calendar.get(Calendar.YEAR), 0, 1);
        setMinTimeOfDay(calendar);

        return calendar.getTime();
    }

    /**
     * 获取指定年的结束时间 注意月份要减1
     *
     * @return
     */
    public static Date getEndTimeOfCurrentYear(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.clear();
        calendar.setTime(date);
        calendar.set(calendar.get(Calendar.YEAR), 11, 31);
        setMaxTimeOfDay(calendar);

        return calendar.getTime();
    }

    /**
     * 设置当天的开始时间
     *
     * @param calendar
     */
    private static void setMinTimeOfDay(Calendar calendar) {
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.MILLISECOND, 0);
    }

    /**
     * 设置当天的结束时间
     *
     * @param calendar
     */
    private static void setMaxTimeOfDay(Calendar calendar) {
        calendar.set(Calendar.HOUR_OF_DAY, 23);
        calendar.set(Calendar.SECOND, 59);
        calendar.set(Calendar.MINUTE, 59);
        calendar.set(Calendar.MILLISECOND, 999);
    }
}
